package com.bestcem.xm.common.core.uitls;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class FileUtil {

    public static final String B = "B";

    public static final String K = "K";

    public static final String M = "M";

    public static final String G = "G";

    public static String getFileExtension(String fileName) {
        if (StringUtils.isBlank(fileName)) {
            return "";
        }
        int index = fileName.lastIndexOf(".");
        if (index > -1) {
            return fileName.substring(index + 1);
        }
        return "";
    }


    public static boolean checkFileSize(Long len, int size, String unit) {
        double fileSize = 0;
        if (B.equalsIgnoreCase(unit)) {
            fileSize = (double) len;
        } else if (K.equalsIgnoreCase(unit)) {
            fileSize = (double) len / 1024;
        } else if (M.equalsIgnoreCase(unit)) {
            fileSize = (double) len / 1048576;
        } else if (G.equalsIgnoreCase(unit)) {
            fileSize = (double) len / 1073741824;
        }
        return !(fileSize > size);
    }
}
